﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>把操纵杆作为鼠标使用 - 脚本示例 | AutoHotkey v2</title>
<meta name="description" content="The joystick-as-mouse script converts a joystick into a three-button mouse." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>把操纵杆作为鼠标使用</h1>

<p>此脚本把操纵杆转换为三键的鼠标. 它允许每个按钮像鼠标按钮那样拖动, 且它几乎不消耗 CPU 时间. 还有, 您将操纵杆推离中央越远, 它会让光标移动的越快. 您可以在脚本的顶部个性化各种设置.
</p>
<p><a href="JoystickMouse.ahk">下载此脚本</a> &nbsp;| &nbsp;<a href="index.htm">其他示例脚本</a> &nbsp;| &nbsp;<a href="../AutoHotkey.htm">主页</a></p>

<pre class="NoIndent"><em>; Increase the following value to make the mouse cursor move faster:</em>
JoyMultiplier := 0.30

<em>; Decrease the following value to require less joystick displacement-from-center
; to start moving the mouse.  However, you may need to calibrate your joystick
; -- ensuring it's properly centered -- to avoid cursor drift. A perfectly tight
; and centered joystick could use a value of 1:</em>
JoyThreshold := 3

<em>; Change the following to true to invert the Y-axis, which causes the mouse to
; move vertically in the direction opposite the stick:</em>
InvertYAxis := false

<em>; Change these values to use joystick button numbers other than 1, 2, and 3 for
; the left, right, and middle mouse buttons.  Available numbers are 1 through 32.
; Use the Joystick Test Script to find out your joystick's numbers more easily.</em>
ButtonLeft := 1
ButtonRight := 2
ButtonMiddle := 3

<em>; If your joystick has a POV control, you can use it as a mouse wheel.  The
; following value is the number of milliseconds between turns of the wheel.
; Decrease it to have the wheel turn faster:</em>
WheelDelay := 250

<em>; If your system has more than one joystick, increase this value to use a joystick
; other than the first:</em>
JoystickNumber := 1

<em>; END OF CONFIG SECTION -- Don't change anything below this point unless you want
; to alter the basic nature of the script.</em>

#SingleInstance

JoystickPrefix := JoystickNumber "Joy"
Hotkey JoystickPrefix . ButtonLeft, "ButtonLeft"
Hotkey JoystickPrefix . ButtonRight, "ButtonRight"
Hotkey JoystickPrefix . ButtonMiddle, "ButtonMiddle"

<em>; Calculate the axis displacements that are needed to start moving the cursor:</em>
JoyThresholdUpper := 50 + JoyThreshold
JoyThresholdLower := 50 - JoyThreshold
if InvertYAxis
    YAxisMultiplier := -1
else
    YAxisMultiplier := 1

SetTimer "WatchJoystick", 10  <em>; Monitor the movement of the joystick.</em>

JoyInfo := GetKeyState(JoystickNumber "JoyInfo")
if InStr(JoyInfo, "P")  <em>; Joystick has POV control, so use it as a mouse wheel.</em>
    SetTimer "MouseWheel", WheelDelay

<em>; The functions below do not use KeyWait because that would sometimes trap the
; WatchJoystick quasi-thread beneath the wait-for-button-up thread, which would
; effectively prevent mouse-dragging with the joystick.</em>

ButtonLeft()
{
    SetMouseDelay -1  <em>; Makes movement smoother.</em>
    MouseClick "Left",,, 1, 0, "D"  <em>; Hold down the left mouse button.</em>
    SetTimer "WaitForLeftButtonUp", 10
    
    WaitForLeftButtonUp()
    {
        if GetKeyState(A_ThisHotkey)
            return  <em>; The button is still, down, so keep waiting.
        ; Otherwise, the button has been released.</em>
        SetTimer, 0
        SetMouseDelay -1  <em>; Makes movement smoother.</em>
        MouseClick "Left",,, 1, 0, "U"  <em>; Release the mouse button.</em>
    }
}

ButtonRight()
{
    SetMouseDelay -1  <em>; Makes movement smoother.</em>
    MouseClick "Right",,, 1, 0, "D"  <em>; Hold down the right mouse button.</em>
    SetTimer "WaitForRightButtonUp", 10
    
    WaitForRightButtonUp()
    {
        if GetKeyState(A_ThisHotkey)
            return  <em>; The button is still, down, so keep waiting.
        ; Otherwise, the button has been released.</em>
        SetTimer, 0
        MouseClick "Right",,, 1, 0, "U"  <em>; Release the mouse button.</em>
    }
}

ButtonMiddle()
{
    SetMouseDelay -1  <em>; Makes movement smoother.</em>
    MouseClick "Middle",,, 1, 0, "D"  <em>; Hold down the middle mouse button.</em>
    SetTimer "WaitForMiddleButtonUp", 10
    
    WaitForMiddleButtonUp()
    {
        if GetKeyState(A_ThisHotkey)
            return  <em>; The button is still, down, so keep waiting.
        ; Otherwise, the button has been released.</em>
        SetTimer, 0
        MouseClick "Middle",,, 1, 0, "U"  <em>; Release the mouse button.</em>
    }

}

WatchJoystick()
{
    global
    MouseNeedsToBeMoved := false  <em>; Set default.</em>
    joyx := GetKeyState(JoystickNumber "JoyX")
    joyy := GetKeyState(JoystickNumber "JoyY")
    if joyx &gt; JoyThresholdUpper
    {
        MouseNeedsToBeMoved := true
        DeltaX := Round(joyx - JoyThresholdUpper)
    }
    else if joyx &lt; JoyThresholdLower
    {
        MouseNeedsToBeMoved := true
        DeltaX := Round(joyx - JoyThresholdLower)
    }
    else
        DeltaX := 0
    if joyy &gt; JoyThresholdUpper
    {
        MouseNeedsToBeMoved := true
        DeltaY := Round(joyy - JoyThresholdUpper)
    }
    else if joyy &lt; JoyThresholdLower
    {
        MouseNeedsToBeMoved := true
        DeltaY := Round(joyy - JoyThresholdLower)
    }
    else
        DeltaY := 0
    if MouseNeedsToBeMoved
    {
        SetMouseDelay -1  <em>; Makes movement smoother.</em>
        MouseMove DeltaX * JoyMultiplier, DeltaY * JoyMultiplier * YAxisMultiplier, 0, "R"
    }
}

MouseWheel()
{
    global
    JoyPOV := GetKeyState(JoystickNumber "JoyPOV")
    if JoyPOV = -1  <em>; No angle.</em>
        return
    if (JoyPOV &gt; 31500 or JoyPOV &lt; 4500)  <em>; Forward</em>
        Send "{WheelUp}"
    else if JoyPOV &gt;= 13500 and JoyPOV &lt;= 22500  <em>; Back</em>
        Send "{WheelDown}"
}
</pre>
</body>
</html>